public class AVLTreeDemo {
    public static void main(String[] args) {
        //int[] array = {4,3,6,5,7,8};
        //int[] array = {10,12,8,9,7,6};
        int[] array = {10,11,7,6,8,9};
        AVLTree avlTree = new AVLTree();

        for(int i = 0; i < array.length; i++){
            avlTree.add(new Node(array[i]));
        }

        System.out.println("中序遍历排序");
        avlTree.infixOrder();

        System.out.println("排序前：");
        System.out.println("AVL树高度:" + avlTree.getRoot().height());
        System.out.println("AVL树左子树高度:" + avlTree.getRoot().leftHeight());
        System.out.println("AVL树右子树高度:" + avlTree.getRoot().rightHeight());
    }
}

//创建二叉树
class AVLTree{
    private Node root;

    public Node getRoot() {
        return root;
    }

    public void add(Node node){
        if(root == null){
            root = node;
        }else{
            root.add(node);
        }
    }

    public void infixOrder(){
        if(root != null){
            root.infixOrder();
        }else{
            System.out.println("二叉树为空无法排序");
        }
    }

    public Node search(int value){
        if(root == null){
            return null;
        }else{
            return root.search(value);
        }
    }

    public Node searchParent(int value){
        if(root == null){
            return null;
        }else{
            return root.searchParent(value);
        }
    }

    /**
     * 找到目标节点下的子树的最小值节点
     * @param node:目标节点
     * @return：返回目标节点下子树的节点的最小值
     */
    public int delRightTreeMin(Node node){
        Node target = node;

        //一直向左找可以找到最小值
        while(target.left != null){
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }

    public void delNode(int value){
        if(root == null){
            return;
        }else{
            Node targetNode = search(value);
            if(targetNode == null){
                return;
            }
            if(root.left == null && root.right == null){
                root = null;
                return;
            }

            //去找父节点
            Node parent = searchParent(value);
            //如果目标节点为叶子结点
            if(targetNode.left == null && targetNode.right == null){
                if(parent.left != null && parent.left.value == value){
                    parent.left = null;
                }else if(parent.right != null && parent.right.value == value){
                    parent.right = null;
                }
            }else if(targetNode.left != null && targetNode.right != null){//目标节点有两个子节点
                int minValue = delRightTreeMin(targetNode.right);
                targetNode.value = minValue;
            }else{//目标节点只有一个子节点

                //当目标节点只有左节点时
                if(targetNode.left != null){
                    if(parent != null) {
                        //当目标节点值等于父节点的左节点时
                        if (parent.left.value == value) {
                            //目标节点左节点赋给父节点的左节点
                            parent.left = targetNode.left;
                        } else {
                            //目标节点左节点赋给父节点的右节点
                            parent.right = targetNode.left;
                        }
                    }else{
                        root = targetNode.left;
                    }
                }else{//当目标节点只有右节点时
                    if(parent != null) {
                        //当目标节点值等于父节点的右节点时
                        if (parent.right.value == value) {
                            //目标节点右节点赋给父节点的右节点
                            parent.right = targetNode.right;
                        } else {
                            //目标节点右节点赋给父节点的左节点
                            parent.left = targetNode.right;
                        }
                    }else{
                        root = targetNode.right;
                    }
                }
            }
        }
    }

}

class Node{
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    public int leftHeight(){
        if(left == null){
            return 0;
        }else{
            return left.height();
        }
    }

    public int rightHeight(){
        if(right == null){
            return 0;
        }else{
            return right.height();
        }
    }

    public int height(){
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    //左旋转
    public void leftRotate(){
        //设置新节点为当前节点，即根节点
        Node newNode = new Node(value);
        //将新节点指向当前节点的左子树
        newNode.left = left;
        //将新节点的右子树设为当前节点的右节点的左子树
        newNode.right = right.left;
        //将当前节点的值赋为当前节点的右节点
        value = right.value;
        //将当前节点的右子树设置成当前节点右子树的右子树
        right = right.right;
        //将当前节点的左子树与新节点相连接
        left = newNode;

    }

    //右旋转
    public void rightRotate(){
        Node newNode = new Node(value);
        newNode.right = right;
        newNode.left = left.right;
        value = left.value;
        left = left.left;
        right = newNode;

    }

    /**
     * 寻找目标节点
     * @param value:目标节点的值
     * @return:返回目标节点
     */
    public Node search(int value){
        if(value == this.value){
            return this;
        } else if(value < this.value){
            if(this.left == null){
                return null;
            }else{
                return this.left.search(value);
            }
        }else {
            if(this.right == null){
                return null;
            }else{
                return this.right.search(value);
            }
        }
    }

    /**
     * 寻找目标节点的父节点
     * @param value:目标节点的值
     * @return:返回目标节点父节点
     */
    public Node searchParent(int value){
        if((this.right != null && this.right.value == value) || (this.left != null && this.left.value == value)){
            return this;
        }else {
            if(this.value > value && this.left != null){
                return this.left.searchParent(value);
            }else if(this.value <= value && this.right != null){
                return this.right.searchParent(value);
            }else{
                return null;
            }
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    public void add(Node node){
        if(node == null){
            return;
        }
        if(node.value < this.value){
            if(this.left == null){
                this.left = node;
            }else{
                this.left.add(node);
            }
        }else{
            if(this.right == null){
                this.right = node;
            }else{
                this.right.add(node);
            }
        }

        //当添加完一个节点，判断是否为AVLTree,左旋转
        if(rightHeight() - leftHeight() > 1 ){
            //右节点的左子树高度大于右子树，先对右节点进行右旋转，再对整个树进行左旋转
            if(right != null && right.leftHeight() > right.rightHeight()){
                right.rightRotate();
                leftRotate();
            }else{
                leftRotate();
            }
            //必须要！！
            return;
        }

        //右旋转
        if(leftHeight() - rightHeight() > 1){
            if(left != null && left.leftHeight() < left.rightHeight()){
                left.leftRotate();
                rightRotate();
            }else{
                rightRotate();
            }
        }

    }

    public void infixOrder(){
        if(this.left != null){
            this.left.infixOrder();
        }

        System.out.println(this);

        if(this.right != null){
            this.right.infixOrder();
        }
    }
}
